package com.cloud.pet.ops.config;

import com.cloud.pet.ops.annotation.CheckToken;
import com.cloud.pet.ops.annotation.PassToken;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * description:swagger配置类
 * Author	Date	Changes
 * tc  2019年1月24日 Created
 */
@Configuration
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")
@EnableSwagger2
public class SwggerConfig {

	@Bean
	public Docket bmsApi() {
		return buildDocket("基础服务","com.cloud.car.dispatch.controller.bms");
	}

	@Bean
	public Docket dmsApi() {
		return buildDocket("渠道中心","com.cloud.car.dispatch.controller.dms");
	}

	@Bean
	public Docket mksApi() {
		return buildDocket("营销中心","com.cloud.car.dispatch.controller.mks");
	}

	@Bean
	public Docket pcsApi() {
		return buildDocket("支付中心","com.cloud.car.dispatch.controller.pcs");
	}

	@Bean
	public Docket pmsApi() {
		return buildDocket("商品中心","com.cloud.car.dispatch.controller.pms");
	}

	@Bean
	public Docket ucsApi() {
		return buildDocket("会员中心","com.cloud.car.dispatch.controller.ucs");
	}
	@Bean
    public Docket passToken() {
        List<Parameter> pars = buildHeader();
        
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("普通请求")
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(PassToken.class))
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(pars)  ;
    }

	private Docket buildDocket(String name,String packageName) {
		ParameterBuilder headerToken = new ParameterBuilder();
		List<Parameter> pars = new ArrayList<>();
		headerToken.name("token").description("用户令牌")
				.modelRef(new ModelRef("string")).parameterType("header")
				.required(true).build();
		pars.add(headerToken.build());
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(name)
				.select()
				.apis(RequestHandlerSelectors.basePackage(packageName))
				.paths(PathSelectors.any())
				.build()
				.globalOperationParameters(pars);
	}
	
    @Bean
    public Docket vilidateToken() {
    	ParameterBuilder headerToken = new ParameterBuilder();
        List<Parameter> pars = buildHeader();
        headerToken.name("token").description("用户令牌")
    	.modelRef(new ModelRef("string")).parameterType("header") 
    	.required(true).build();
    	pars.add(headerToken.build());
     	
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.withMethodAnnotation(CheckToken.class))
            .paths(PathSelectors.any())
            .build()
            .globalOperationParameters(pars)  ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("中台服务接口")
            .version("v1")
            .build();
    }

    private List<Parameter> buildHeader(){
        List<Parameter> pars = new ArrayList<Parameter>();
    	ParameterBuilder headerAppkey = new ParameterBuilder();
    	headerAppkey.name("app_id").description("应用ID")
    	.modelRef(new ModelRef("string")).parameterType("header") .defaultValue("2B14A4DB674013075FCBE4D1AF1F607B7E215C04A9984CC84B0792D6F1E6F6D4")
    	.required(true).build();
    	pars.add(headerAppkey.build());
    	
    	ParameterBuilder headerVersion = new ParameterBuilder();
    	headerVersion.name("app_version").description("App版本号").defaultValue("1.0")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerVersion.build());
    	
    	ParameterBuilder headerChannel = new ParameterBuilder();
    	headerChannel.name("channel").description("渠道").defaultValue("appStore")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerChannel.build());
    	
    	ParameterBuilder headerPlatform = new ParameterBuilder();
    	headerPlatform.name("platform").description("平台").defaultValue("ios")
    	.modelRef(new ModelRef("string")).parameterType("header") 
    	.required(true).build();
    	pars.add(headerPlatform.build());
    	
    	ParameterBuilder headerDevice = new ParameterBuilder();
    	headerDevice.name("device_id").description("设备编号").defaultValue("sdfafadsfadsfafadfafassssssssssss")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerDevice.build());
    	
    	ParameterBuilder headerModel = new ParameterBuilder();
    	headerModel.name("mobile_model").description("手机型号").defaultValue("iphone7")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerModel.build());
    	
    	ParameterBuilder headerLat = new ParameterBuilder();
    	headerLat.name("lat").description("经度").defaultValue("100.156161")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerLat.build());
    	
    	ParameterBuilder headerLng= new ParameterBuilder();
    	headerLng.name("lng").description("纬度").defaultValue("100.156161")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerLng.build());
    	
    	ParameterBuilder headerOs= new ParameterBuilder();
    	headerOs.name("os_version").description("操作系统版本").defaultValue("12.1.1")
    	.modelRef(new ModelRef("string")).parameterType("header").build();
    	pars.add(headerOs.build());
		
    	return pars;
    }
}
